1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class BallControl : MonoBehaviour {
6
7     
private Rigidbody2D rb2d;
8     
private Vector2 vel;
9
10     
void GoBall() {
11         
float rand = Random.Range (0, 2);
12         
if (rand < 1) {
13             rb2d.AddForce (
new Vector2 (20, -15));
14         }
else {
15             rb2d.AddForce (
new Vector2 (-20, -15));
16         }
17     }
18
19     
// Use this for initialization
20     
void Start () {
21         rb2d = GetComponent<Rigidbody2D> ();
22         Invoke (
"GoBall", 2);
23     }
24
25     
void ResetBall() {
26         vel =
new Vector2 (0, 0);
27         rb2d.velocity = vel;
28         transform.position = Vector2.zero;
29     }
30
31     
void RestartGame() {
32         ResetBall ();
33         Invoke (
"GoBall", 1);
34     }
35
36     
void OnCollisionEnter2D(Collision2D coll) {
37         
if (coll.collider.CompareTag ("Player")) {
38             vel.x = rb2d.velocity.x;
39             vel.y = (rb2d.velocity.y /
2.0f) + (coll.collider.attachedRigidbody.velocity.y / 3.0f);
40             rb2d.velocity = vel;
41         }
42     }
43
44 }


Use this for initialization



Gõ tìm kiếm nhanh...